package com.atguigu.gulimall.member.web;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.entity.MemberReceiveAddressEntity;
import com.atguigu.gulimall.member.feign.OrderFeignService;
import com.atguigu.gulimall.member.service.MemberReceiveAddressService;
import com.atguigu.gulimall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class MemberWebController {

    @Autowired
    OrderFeignService orderFeignService;
    @Autowired
    MemberService memberService;
    @Autowired
    MemberReceiveAddressService memberReceiveAddressService;


    @GetMapping("/memberOrder.html")
    public String memberOrderPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Model model){

        //查出当前登录用户的所有订单列表数据
        Map<String,Object> page=new HashMap<>();
        page.put("page",pageNum.toString());
        R r = orderFeignService.listWithItem(page);
        model.addAttribute("orders",r);
        System.out.println(JSON.toJSON(r));
        return "orderList";
    }

    @GetMapping("/getAllById")
    public String getAllById(Model model, HttpServletRequest request){
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        Long id1 = attribute.getId();
        System.out.println("getAllById,id"+id1);
        MemberEntity member = memberService.getById(id1);
        System.out.println("member"+member);
        model.addAttribute("member",member);
        return "udai_setting";
    }

    @GetMapping("/settingUpdateGetAll")
    public String settingUpdateGetAll(Model model,HttpServletRequest request){
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        Long id1 = attribute.getId();
        System.out.println("settingUpdateGetAll,id"+id1);
        MemberEntity member = memberService.getById(id1);
        model.addAttribute("member",member);
        return "udai_setting_update";
    }

    @PostMapping("/settingUpdate")
    public boolean settingUpdate(MemberEntity member,HttpServletRequest request,Model model){
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        Long id1 = attribute.getId();
        member.setId(id1);
        boolean b = memberService.updateById(member);
//        if (b){
//            return "redirect:/getAllById";
//        }else {
//            return "redirect:/settingUpdateGetAll";
//        }
        return b;
    }

    @GetMapping("/getAllAddress")
    public String getAllAddress(Model model,HttpServletRequest request){
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        Long id1 = attribute.getId();
        System.out.println("settingUpdateGetAll,id"+id1);
        MemberReceiveAddressEntity addressEntity = memberReceiveAddressService.getById(id1);
        model.addAttribute("addressEntity",addressEntity);
        return "udai_address";
    }

}
